package top.wugy.blog;

import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.template.Engine;
import top.wugy.blog.kits.Const;
import top.wugy.blog.model.*;
import top.wugy.blog.routes.AssistController;
import top.wugy.blog.routes.BlogController;
import top.wugy.blog.routes.BlogDirective;
import top.wugy.blog.routes.FrontController;
import top.wugy.blog.searcher.LuceneSearcher;
import top.wugy.blog.searcher.SearcherKit;
import top.wugy.blog.searcher.SearcherPlugin;
import top.wugy.blog.web.GlobalInterceptor;

/**
 * wugy on 2017/11/2 15:19
 */
public class BlogApp extends JFinalConfig {

    private WallFilter wallFilter;

    static {
        PropKit.use("config.properties");
    }

    @Override
    public void configConstant(Constants me) {
        me.setDevMode(Const.DEV_MODE);
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", FrontController.class);
        me.add("/blog", BlogController.class);
        me.add("/assist", AssistController.class);
    }

    @Override
    public void configEngine(Engine me) {
        me.addDirective("blogDirective", new BlogDirective());
    }

    @Override
    public void configPlugin(Plugins me) {
        DruidPlugin dp = new DruidPlugin(Const.DB_URL, Const.DB_USERNAME,
                Const.DB_PWD);
        wallFilter = new WallFilter(); // 加强数据库安全
        wallFilter.setDbType(JdbcUtils.MYSQL);
        dp.addFilter(wallFilter);
        me.add(dp);

        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        arp.setShowSql(Const.DEV_MODE);
        arp.addMapping(Const.TABLE_BLOG, Const.PK_FIELD, TBlog.class);
        arp.addMapping(Const.TABLE_TAG, Const.PK_FIELD, TTag.class);
        arp.addMapping(Const.TABLE_USER, Const.PK_FIELD, TUser.class);
        arp.addMapping(Const.TABLE_LINK, Const.PK_FIELD, TLink.class);
        arp.addMapping(Const.TABLE_TIMELINE, Const.PK_FIELD, TTimeLine.class);
        me.add(arp);

        me.add(new RedisPlugin(Const.REDIS_CACHE, Const.REDIS_HOST));

        me.add(new SearcherPlugin(new LuceneSearcher()));
    }

    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new GlobalInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("ctx"));
    }


    @Override
    public void afterJFinalStart() {
        // 系统启动后，设置登录失效
        Redis.use(Const.REDIS_CACHE).del(Const.LOGIN_TIME);
        wallFilter.getConfig().setSelectUnionCheck(false);
        new Thread(() -> SearcherKit.reloadIndex()).start();
    }

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", Const.PORT, "/");
    }
}
